package com.feizhoutuobang.shapeline.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.feizhoutuobang.shapeline.bo.Feed;
import com.feizhoutuobang.shapeline.dao.FeedDao;
import com.feizhoutuobang.shapeline.util.Calendar;

public class FeedDaoImpl extends HibernateDaoSupport implements FeedDao {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Feed> getFeedsToFetch() {
		Date t = new Date();
		String now = Calendar.format(t);
		String hql = "from Feed where nextFetchTime_<=:now and valid=true";
		Query query = getSession().createQuery(hql).setString("now", now);
		
		query.setFirstResult(0);
		query.setFetchSize(20);
		
		return query.list();
	}

	@Override
	public void updateFeed(Feed feed) {
		getHibernateTemplate().update("Feed", feed);
	}

	@Override
	public Feed getFeedById(String id) {
		String hql = "from Feed where id=:id";
		Query query = getSession().createQuery(hql).setString("id", id);
		return (Feed)query.uniqueResult();
	}

}
